﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class UAgentMacKeepList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.删除, Permission.新增, Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UAgentMacKeepList";
            }
        }
        #region 属性 (查询条件)





        protected string ShowAgentName
        {
            get
            {
                return base.User.UserType==2 ? OUserController.MutlCmpStr(base.User.UserId) : "";

            }
        }

        protected string searchAgentName
        {
            get
            {
                return GetRequestValue<string>("searchAgentName", "");

            }
        }

        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }


        protected string IntoDateFrom
        {
            get
            {
                return GetRequestValue<string>("IntoDateFrom", "");

            }
        }

        protected string IntoDateTo
        {
            get
            {
                string temp = GetRequestValue<string>("IntoDateTo", "");
                if (string.IsNullOrEmpty(IntoDateFrom))
                    return "";
                if (string.IsNullOrEmpty(temp))
                {
                    temp = Convert.ToDateTime(IntoDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                }

                return temp;

            }
        }

        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "UMKID");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }
        #endregion

        protected string GetSqlWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(ShowAgentName))
            {


                sqlcommonFiled = string.Format("AgentName in {0}", ShowAgentName);// QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, AgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(searchAgentName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, searchAgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            if (IntoDateFrom != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoDate", QueryOperatorsEnum.范围, IntoDateFrom + "|" + IntoDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            return wheresql;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        protected DataTable sumdt = null;
        protected void DataBind()
        {
            DataTableCollection dtc = UMacKeepController.GetPagination(GetSqlWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataSource = dtc[0];
            this.rpMenu.DataBind();

            sumdt = dtc[1];

            AddLog(Permission.查看);
        }

        #region
        

        protected string GetPer(object x1, object x2)
        {
            if (x1 is DBNull || x2 is DBNull)
                return "0";
            if (x2.ToString() == "0")
                return "0";

            return (Convert.ToDecimal(x1) / Convert.ToDecimal(x2) * 100).ToString("F0");
        }


        private List<UMacKeepDetailInfo> _detailList = null;
        private List<UMacKeepDetailInfo> DetailList
        {
            get {
                return _detailList;
            }
            set {
                _detailList = value;
            }
        }
        protected string GetUMacKeepDetail(object SerialNumber, object KeepHours)
        {
            string _SerialNumber = SerialNumber.ToString();
            int _KeepHours = Convert.ToInt32(KeepHours);

            if (_detailList == null)
            {
                DetailList = UMacKeepController.UMacKeepDetail_GetBySerialNumber(_SerialNumber);
            }

            if(DetailList.Count(e=>e.SerialNumber.ToUpper()==_SerialNumber.ToUpper())<=0)
            {
                DetailList = UMacKeepController.UMacKeepDetail_GetBySerialNumber(_SerialNumber.ToUpper());               
            }

            UMacKeepDetailInfo _detail = DetailList.Find(e => e.KeepHours == _KeepHours);
            _detail = _detail == null ? new UMacKeepDetailInfo() : _detail;
            if (string.IsNullOrEmpty(_detail.SerialNumber))
            {
                return string.Format( "<a class='add' href='SvrMgr/UAgentMacKeepDetailAdd.aspx?Action=add&KeepHours={0}&SerialNumber={1}' target='dialog' title='添加保养记录' rel='UMacKeepDetailAdd' max=false  mask=true  maxable=false  minable=false  resizable=false drawable=true  width=450 height=450>保养</a>",_KeepHours,_SerialNumber);
            }
            else
            {
                return string.Format("<a class='add' href='SvrMgr/UAgentMacKeepDetailAdd.aspx?Action=update&UMKDID={0}' target='dialog' title='修改保养记录' rel='UMacKeepDetailAdd' max=false  mask=true  maxable=false  minable=false  resizable=false drawable=true  width=450 height=450 style='color:{2}' >{1}</a>",_detail.UMKDID,_detail.WorkTime.ToString()+" | "+_detail.KeepDate.ToString(),_detail.IsVerify==1?"":"red");
            }
            // List<UMacKeepDetailInfo> _detailList = UMacKeepController.UMacKeepDetail_GetBySerialNumber(_SerialNumber);

            // if()

        }

        #endregion
    }
}